package com.open.party.cloud.view.task.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.TaskBean;
import com.open.party.cloud.model.TaskBean;
import com.sinothk.android.utils.DateUtil;
import com.sinothk.android.utils.XUtils;
import com.sinothk.view.image.rounded.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class TaskListForMeAdapter extends RecyclerView.Adapter<TaskListForMeAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<TaskBean> mData;
    private Context mContext;
    private OnSuperListener<TaskBean> superListener;

    public TaskListForMeAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<TaskBean> superListener) {
        this.superListener = superListener;
    }

    public void setData(List<TaskBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<TaskBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.task_list_for_me_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final TaskBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getTitle()));

        holder.levelTv.setText(itemData.getUrgencyType().getDesc());
        if ("普通".equals(itemData.getUrgencyType().getDesc())) {
            holder.levelTv.setBackgroundResource(R.drawable.shape_level_bg_blue_light);
        } else if ("紧急".equals(itemData.getUrgencyType().getDesc())) {
            holder.levelTv.setBackgroundResource(R.drawable.shape_level_bg_orange_light);
        } else {
            holder.levelTv.setBackgroundResource(R.drawable.shape_level_bg_pink);
        }

//        holder.statusTv.setText("完成度：" + itemData.getRate());
//        JXZ("进行中"),
//        SHZ("审核中"),
//        YTH("已退回"),
//        YWC("已完成");
//        YYQ("已延期"),
//        if ("JXZ".equals(itemData.getState().getCode())) {
//            holder.statusTv.setText("状态：" + "进行中");
//        } else if ("SHZ".equals(itemData.getState().getCode())){
//            holder.statusTv.setText("状态：" + "审核中");
//        } else if ("YTH".equals(itemData.getState().getCode())){
//            holder.statusTv.setText("状态：" + "已退回");
//        } else if ("YWC".equals(itemData.getState().getCode())){
//            holder.statusTv.setText("状态：" + "已完成");
//        } else if ("YYQ".equals(itemData.getState().getCode())){
//            holder.statusTv.setText("状态：" + "已延期");
//        } else {
//            holder.statusTv.setText("状态：" + "未定义");
//        }

        if (XUtils.string().isNotEmpty(itemData.getState().getDesc())) {
            holder.statusTv.setText("状态：" + itemData.getState().getDesc());
        }else{
            holder.statusTv.setText("状态：" + "未定义");
        }

        holder.timeTv.setText("完成期限：" + XUtils.date().getDateStrByDate(itemData.getEndTime(), "yyyy/MM/dd"));

        if (superListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    superListener.onClick(position, itemData, "");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, levelTv, statusTv, timeTv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            levelTv = itemView.findViewById(R.id.levelTv);
            statusTv = itemView.findViewById(R.id.statusTv);
            timeTv = itemView.findViewById(R.id.timeTv);
        }
    }
}